<template>
  <div>
    <el-form :model="form" ref="form" label-width="120px" :rules="rules">
      <el-form-item label="题库名" prop="bankName">
        <el-input v-model="form.bankName" />
      </el-form-item>
      <el-form-item v-if="$route.path === '/question/privateBank' && id != null" label="是否为公共题库">
        <el-switch v-model="form.isCommon"></el-switch>
      </el-form-item>
      <el-form-item label="展示封面">
        <el-upload
            action="#"
            :auto-upload="false"
            :limit="1"
            :multiple="false"
            list-type="picture-card"
            accept="image/*"
            :on-change="changeFile"
            :on-remove="removeFile"
            :file-list="fileList"
            :class="{hide: hideUpload}">
          <i class="el-icon-plus"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.remark" />
      </el-form-item>
      <el-form-item>
        <el-button type="success" size="small" @click="submit">提交</el-button>
        <el-button type="danger" size="small" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { get, post } from "@/plugins/axios"
import {Message} from "element-ui"

export default {
  name: "edit",
  props: ['id'],
  created() {
    if (this.id != null) {
      this.getOne()
    }
  },
  data() {
    const module = '/questionBank'
    return {
      uri: {
        getOne: module + '/selectOne/',
        saveOrUpdate: module + '/saveOrUpdate',
        saveOrUpdateAudit: module + '/saveOrUpdateAudit'
      },
      form: {
        userId: this.$store.getters.GET_ACCOUNT.id,
        bankName: '',
        isCommon: false,
        imgPath: '',
        remark: ''
      },
      rules: {
        bankName: [
          {required: true, message: '请输入题库名', trigger: 'blur'}
        ]
      },
      fileList: [],
      hideUpload: false
    }
  },
  methods: {
    submit() {
      let url = this.uri.saveOrUpdate
      // 如果添加公共题库则需要走题库审批 状态：1
      // 如果添加私有题库则不需要题库审批 状态：0
      // 如果将私有题库分享到公共题库也需要审批 状态：2
      if (this.$route.path === '/question/commonBank') {
        this.form.isCommon = 1
      } else if (this.$route.path === '/question/privateBank' && this.id == null) {
        this.form.isCommon = 0
      } else if (this.$route.path === '/question/privateBank' && this.id != null) {
        // 先判断当前题库是否为公共题库  否的话 发送审批url 是的话不走审批流程
        this.form.isCommon = this.form.isCommon ? 2 : 0
        // 私人题库 -> 公共题库
        if (this.form.isCommon === 2) {
          url = this.uri.saveOrUpdateAudit
        }
      }

      this.$refs.form.validate((valid) => {
        if (valid) {
          post(url, this.form, response => {
            if (response.code === 200) {
              Message.success(response.message)
            }
            this.$emit('update:editDialogShow',false)
            this.$emit('getQuestionBankData')
          })
        } else {
          return false
        }
      });
    },
    cancel() {
      this.$emit('update:editDialogShow',false)
    },
    getOne() {
      get(this.uri.getOne + this.id, {}, response => {
        for (const key in this.form) {
          this.form[key] = response.data[key]
        }
        this.form.id = response.data.id
        this.form.isCommon = (this.form.isCommon === 1 || this.form.isCommon === 2)
        this.fileList.push({url: this.imgUrl + this.form.imgPath})
        if (this.fileList[0].url) {
          this.hideUpload = true
        } else {
          this.hideUpload = false
        }
      })
    },
    changeFile(file) {
      this.form.imgFile = file.raw
      this.hideUpload = true
    },
    removeFile() {
      this.form.imgFile = null
      this.hideUpload = false
    }
  }
}
</script>

<style scoped lang="less">
.hide {
  /deep/
  .el-upload--picture-card {
    display: none;
  }
}
</style>